package com.hualan.servlet;

import com.alibaba.fastjson.JSON;
import com.hualan.bean.PanghuShop;
import com.hualan.dao.impl.MenDianDaoImpl;
import com.hualan.dao.MenDianDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet("/MenDian/add")
public class addMenDianServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //得到前端传过来的数据
        //店铺名称
        String shopName = request.getParameter("shopName");
        //店铺描述
        String shopDescribe = request.getParameter("shopDescribe");
        //店铺头像
        String shopImage = request.getParameter(" shopImage");
        //店铺电话
        String shopPhone = request.getParameter("shopPhone");
        //店铺所在省份
        String shopProvince = request.getParameter("shopProvince");
        //店铺所在省份市区
        String shopCity = request.getParameter("shopCity");
        //店铺所在省份市区某个县区
        String shopDistrict = request.getParameter("shopDistrict");
        //店铺所在省份市区某个县区详细地址
        String shopAddress = request.getParameter("shopAddress");
        //店铺位置的经纬度
        String shopLatitude = request.getParameter("shopLatitude");
        String shopLongitude = request.getParameter("shopLongitude");
        //封装门店数据对象
        System.out.println(shopName+"111111111");
        System.out.println(shopLongitude+"222222222");
        PanghuShop panghuShop = new PanghuShop(shopName, shopDescribe, shopImage, shopPhone, shopProvince, shopCity, shopDistrict, shopAddress, shopLatitude, shopLongitude);
        //调用数据库添加对象
        MenDianDaoImpl menDianDao = new MenDianDaoImpl();
        HashMap<String, Object> map = new HashMap<>();
        int row = menDianDao.addMenDian(panghuShop);
        if (row > 0) {
            map.put("code", 200);
            map.put("message", "增加成功");
        } else {
            map.put("code", 300);
            map.put("message", "增加失败");
        }
        //将数据返回到前台页面
        response.getWriter().write(JSON.toJSONString(map));
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
